package com.af.monitorModule.controller.company;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Company;
import com.af.monitorModule.generator.entity.TimedTask;
import com.af.monitorModule.generator.service.CompanyService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @GetMapping("/findCom")
    public JSONObject getAllAccount(String page, String limit, String search){
        JSONObject searchData = JSONObject.parseObject(search);
        return companyService.findCom(page,limit,searchData);
    }

    @GetMapping("/findAllCom")
    public JSONObject findAllCom(){
        return companyService.findAllCom();
    }

    @GetMapping("/findAllComQR")
    public JSONObject findAllComQR(){
        return companyService.findAllComQR();
    }

    @GetMapping("/findAllServer/{id}")
    public JSONObject findAllServer(@PathVariable String id){
        return companyService.findAllServer(id);
    }


    @PostMapping("/add")
    public JSONObject addCompany(@RequestBody Company company){
        if (company == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        company.setState(1);
        if (companyService.save(company)){
            return ResultEnum.OK.getResult();
        }else {
            return ResultEnum.CREATE_ERROR.getResult();
        }
    }

    @PostMapping("/changeState/{id}")
    public JSONObject changeState(@PathVariable("id") String id){
        return companyService.changeState(id);
    }

    @PostMapping("/update/{id}")
    public JSONObject update(@RequestBody Company company, @PathVariable String id){
        return companyService.updateCompany(company,id);
    }

    @PostMapping("/batchChange")
    public JSONObject batchStop(@RequestBody JSONObject ids){
        return companyService.batchStop(ids);
    }
}
